package springboot.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import springboot.bean.Task;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 只有一个地方能更新数据库,异步更新数据库会出现问题
 */
@Slf4j
public class SimpleServerHandler extends ChannelInboundHandlerAdapter {

    public static List<Channel> channels = new ArrayList<>();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel inChannel=ctx.channel();
        if(channels.size()==0){//如果没有连接就进行连接就对了
            channels.add(inChannel);
            System.out.println("=== " + ctx.channel().remoteAddress() + " is active ===");
        }
    }

    public boolean LOAD2_RELEASE;
    public boolean LOAD3_RELEASE;
    public boolean LOAD4_RELEASE;
    public boolean LOAD5_RELEASE;
    public boolean LOAD6_RELEASE;
    public boolean LOAD7_RELEASE;
    public boolean LOAD8_RELEASE;
    public boolean LOAD9_RELEASE;

    private RestTemplate restTemplate=new RestTemplate();
    Task task=new Task();
    int code;
    public static byte[] data=new byte[10];
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws InterruptedException, IOException {
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        /*没有任务在进行中,并且校验通过*/
        if (bytes.length==31){
            if (bytes[0]==2&&bytes[1]==27&&bytes[2]==71&&bytes[30]==3){
                //1002-1001有取货请求,并且之能进入一次,当请求为0时,可以再次进入此循环,创建任务至数据库
                if (bytes[4]==1&&LOAD2_RELEASE==false){//并且该货位没有被使用(没有被使用的意思就是该在数据库中的状态不能为0，1，2，5)
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1002",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1002");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[5] & 0xFF)*256 + ((byte)bytes[6] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD2_RELEASE=true;
                        }
                    }
                }
                if (bytes[4]==0){
                    LOAD2_RELEASE=false;
                }
                //1003-1001
                if (bytes[7]==1&&LOAD3_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1003",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1003");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[8] & 0xFF)*256 + ((byte)bytes[9] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD3_RELEASE=true;
                        }
                    }
                }
                if (bytes[7]==0){
                    LOAD3_RELEASE=false;
                }
                //1004-1001
                if (bytes[10]==1&&LOAD4_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1004",Integer.class);
                    if (res==0) {
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1004");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[11] & 0xFF)*256 + ((byte)bytes[12] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD4_RELEASE=true;
                        }
                    }
                }
                if (bytes[10]==0){
                    LOAD4_RELEASE=false;
                }
                //1005-1001
                if (bytes[13]==1&&LOAD5_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1005",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1005");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[14] & 0xFF)*256 + ((byte)bytes[15] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD5_RELEASE=true;
                        }
                    }
                }
                if (bytes[13]==0){
                    LOAD5_RELEASE=false;
                }
                //1006-1001
                if (bytes[16]==1&&LOAD6_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1006",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1006");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[17] & 0xFF)*256 + ((byte)bytes[18] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD6_RELEASE=true;
                        }
                    }
                }
                if (bytes[16]==0){
                    LOAD6_RELEASE=false;
                }
                //1007-1001
                if (bytes[19]==1&&LOAD7_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1007",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1007");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[20] & 0xFF)*256 + ((byte)bytes[21] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD7_RELEASE=true;
                        }
                    }
                }
                if (bytes[19]==0){
                    LOAD7_RELEASE=false;
                }
                //1008-1001
                if (bytes[22]==1&&LOAD8_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1008",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1008");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[23] & 0xFF)*256 + ((byte)bytes[24] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD8_RELEASE=true;
                        }
                    }
                }
                if (bytes[22]==0){
                    LOAD8_RELEASE=false;
                }
                //1009-1001
                if (bytes[25]==1&&LOAD9_RELEASE==false){
                    Integer res=restTemplate.getForObject("http://localhost:9998/getCount?start=1009",Integer.class);
                    if (res==0){
                        task.setName("TASK-"+ UUID.randomUUID());
                        task.setStart("1009");
                        task.setEnd("1001");
                        task.setStatus(0);
                        code=((byte)bytes[26] & 0xFF)*256 + ((byte)bytes[27] & 0xFF);
                        task.setCode(code);
                        String response=restTemplate.postForObject("http://localhost:9998/createTask",task,String.class);
                        if (response.equals("success")){
                            LOAD9_RELEASE=true;
                        }
                    }
                }
                if (bytes[25]==0){
                    LOAD9_RELEASE=false;
                }
            }
        }
        //Manual recycling to prevent memory leakage;
        ReferenceCountUtil.release(buf);

        data[0]=(byte)255;

        /**
         * 报文发送数据
         */
        data[0] = 2;//报文头
        data[1] = 13;//有效的数据长度
        data[2] = 71;//标志位
        data[3]= (byte) (data[3]+1);//data[3]作为心跳存在
        data[8] = 0;//和校验
        data[9] = 3;//报文尾

        ByteBuf json = Unpooled.wrappedBuffer(data);
        ctx.writeAndFlush(json);
    }
}
